package com.ruoyi.answer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.answer.domain.ExamPaper;
import com.ruoyi.answer.domain.vo.ExamPaperVo;
import com.ruoyi.answer.mapper.ExamPaperMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.user.domain.vo.UcenterMemberVo;
import com.ruoyi.user.mapper.UcenterMemberMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.answer.domain.bo.ExamRecordBo;
import com.ruoyi.answer.domain.vo.ExamRecordVo;
import com.ruoyi.answer.domain.ExamRecord;
import com.ruoyi.answer.mapper.ExamRecordMapper;
import com.ruoyi.answer.service.IExamRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 考试记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-23
 */
@RequiredArgsConstructor
@Service
public class ExamRecordServiceImpl implements IExamRecordService {

    private final ExamRecordMapper baseMapper;
    private final UcenterMemberMapper memberMapper;
    private final ExamPaperMapper paperMapper;

    /**
     * 查询考试记录
     */
    @Override
    public ExamRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询考试记录列表
     */
    @Override
    public TableDataInfo<ExamRecordVo> queryPageList(ExamRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamRecord> lqw = buildQueryWrapper(bo);
        Page<ExamRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<ExamRecordVo> records = result.getRecords();

        List<ExamRecordVo> list=  getExamRecordVos(records);

        result.setRecords(list);
        return TableDataInfo.build(result);
    }

    private List<ExamRecordVo> getExamRecordVos(List<ExamRecordVo> records) {
        List<ExamRecordVo> collect = records.stream().map(item -> {
            UcenterMemberVo ucenterMemberVo = memberMapper.selectVoById(item.getEmployeeAccount());
            ExamPaperVo examPaperVo = paperMapper.selectVoById(item.getPaperId());
            item.setNickname(ucenterMemberVo.getNickname());
            item.setPaperName(examPaperVo.getPaperName());
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查询考试记录列表
     */
    @Override
    public List<ExamRecordVo> queryList(ExamRecordBo bo) {
        LambdaQueryWrapper<ExamRecord> lqw = buildQueryWrapper(bo);
        List<ExamRecordVo> recordVos = baseMapper.selectVoList(lqw);
        List<ExamRecordVo> examRecordVos = getExamRecordVos(recordVos);
        return examRecordVos;
    }

    private LambdaQueryWrapper<ExamRecord> buildQueryWrapper(ExamRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeAccount() != null, ExamRecord::getEmployeeAccount, bo.getEmployeeAccount());
        return lqw;
    }

    /**
     * 新增考试记录
     */
    @Override
    public Boolean insertByBo(ExamRecordBo bo) {
        ExamRecord add = BeanUtil.toBean(bo, ExamRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改考试记录
     */
    @Override
    public Boolean updateByBo(ExamRecordBo bo) {
        ExamRecord update = BeanUtil.toBean(bo, ExamRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除考试记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
